package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.dto.condition.ProductApiSearchCondition;
import com.treasure.mall.biz.dto.product.NormalProductSaveDTO;
import com.treasure.mall.biz.entity.product.ProductPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.product.ProductService;
import com.treasure.mall.biz.vo.product.ProductApiDetailVo;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.ienum.product.ProductEnums;
import com.treasure.mall.zbqmallapi.annotation.AccessControl;
import com.treasure.mall.zbqmallapi.vo.product.IdentificationOrganizationsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author wm
 */
@Slf4j
@RequestMapping("/product")
@RestController
@Api(tags = "商品模块")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Value("${oss.domain}")
    private String resourceDomain;

    @ApiOperation("商品列表")
    @PostMapping("/search")
    @CrossOrigin
    public ApiResult search(ProductApiSearchCondition condition) {
        return ApiResult.ok(productService.search(condition));
    }

    @ApiOperation("商品详情")
    @PostMapping("/getDetail")
    public ApiResult<ProductApiDetailVo> getDetail(Long productId) {
        return ApiResult.ok(productService.getDetail(productId));
    }

    /**
     * 添加普通商品
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加商品")
    @PostMapping("/save")
    @AccessControl(needLogin = true)
    public ApiResult save(@CurrentUser UserPO user, NormalProductSaveDTO dto) {
        dto.setProductType(ProductEnums.ProductType.NORMAL.getCode());
        dto.setMerchantId(user.getMerchantId());
        dto.setSaleType(ProductEnums.SaleType.NORMAL);
        ApiResult apiResult = productService.saveNormalProduct(dto);
        if (apiResult.isSuccess()) {
            return ApiResult.ok();
        }
        return ApiResult.error(apiResult.getResultMsg());
    }

    /**
     * 删除商品
     *
     * @param user
     * @param productId
     * @return
     */
    @ApiOperation("删除商品")
    @PostMapping("/del")
    @AccessControl(needLogin = true)
    public ApiResult del(@CurrentUser UserPO user, Long productId) {
        ProductPO po = productService.findById(productId);
        if (po == null) {
            return ApiResult.error("不存在");
        }
        if (!po.getMerchantPO().getMerchantId().equals(user.getMerchantId())) {
            return ApiResult.error("非法的操作");
        }
        return productService.delProduct(productId);
    }

    /**
     * 上下架
     *
     * @param user
     * @param productId  商品id
     * @param statusCode 1：上架，0：下架
     * @return
     */
    @ApiOperation("上下架")
    @PostMapping("/changeStatus")
    @AccessControl(needLogin = true)
    public ApiResult changeStatus(@CurrentUser UserPO user, Long productId, Integer statusCode) {
        ProductPO po = productService.findById(productId);
        if (po == null) {
            return ApiResult.error("不存在");
        }
        if (!po.getMerchantPO().getMerchantId().equals(user.getMerchantId())) {
            return ApiResult.error("非法的操作");
        }
        return productService.changeStatus(productId, statusCode);
    }

    /**
     * 是否取消推荐
     *
     * @param user
     * @param productId
     * @param top       1  置顶 0 取消置顶
     * @return
     */
    @ApiOperation("是否取消推荐")
    @PostMapping("/changeTopStatus")
    @AccessControl(needLogin = true)
    public ApiResult changeTopStatus(@CurrentUser UserPO user, Long productId, Integer top) {
        ProductPO po = productService.findById(productId);
        if (po == null) {
            return ApiResult.error("不存在");
        }
        if (!po.getMerchantPO().getMerchantId().equals(user.getMerchantId())) {
            return ApiResult.error("非法的操作");
        }
        return productService.changeTopStatus(productId, top);
    }


    /**
     * 检查商品是否有鉴宝费
     *
     * @param user
     * @param productId
     * @return
     */
    @ApiOperation("检查商品是否有鉴宝费")
    @GetMapping("/identificationOrganizations/findFromUser/{id}")
    public ApiResult identificationOrganizations(@CurrentUser UserPO user, @PathVariable("id") Long productId) {
        IdentificationOrganizationsVO vo = new IdentificationOrganizationsVO(0, BigDecimal.ZERO);
        return ApiResult.ok(vo);
    }
}